package com.codemart.service.impl;

import com.codemart.dto.RoleDTO;
import com.codemart.dto.UserDTO;
import com.codemart.mapper.SystemMapper;
import com.codemart.service.SystemService;
import com.codemart.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;

@Service
public class SystemServiceImpl implements SystemService {

    @Autowired
    SystemMapper systemMapper;

    @Override
    public List<RoleVO> getRoleList(Integer id) {
        List<RoleDTO> roleList =  systemMapper.getRoleList(id);
        List<RoleVO> roleVOList = new ArrayList<>();
        roleList.forEach(r -> {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(r,roleVO);

            if(r.getStatus() == 0){
                roleVO.setStatus("禁用");
            }else if(r.getStatus() == 1){
                roleVO.setStatus("启用");
            }

            if(r.getRole_ids() != null){
                roleVO.setRole_ids(Arrays.stream(r.getRole_ids().split(",")).toList());
            }
            if(r.getRole_names() != null){
                roleVO.setRole_names(Arrays.stream(r.getRole_names().split(",")).toList());
            }
            roleVOList.add(roleVO);
        });
        return roleVOList;
    }

    @Override
    public List<ProjectInfoVO> getProjectInfoByTitle(String title) {
        if(title != null){
            title = title.replace("\"","");
        }
        List<ProjectInfoVO> projectInfoByTitle = systemMapper.getProjectInfoByTitle(title);
        System.out.println(projectInfoByTitle);
        return projectInfoByTitle;
    }

    @Override
    public List<UserVO> getUserList(String search) {
        List<UserDTO> userDTOList = new ArrayList<>();
        search = search.replace("\"","");
        if(search.equals("") || search.equals("null")){
            search = null;
        }
        userDTOList= systemMapper.listUserAll(search);
        List<UserVO> userList = new ArrayList<>();
        userDTOList.forEach(user -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user,userVO);
            userList.add(userVO);
        });
        return userList;
    }

    @Override
    public void addRole(String url, String msg) {
        systemMapper.addRole(url,msg);
    }

    @Override
    public List<UserRoleVO> getUserRole(String search) {
        List<UserRoleVO> userRoleList = systemMapper.getUserRoleList(search);
        return userRoleList;
    }

    @Override
    public void removeRolePermission(Set<Integer> ids, Integer id) {
        ids.forEach(i -> {
            systemMapper.removeRolePermission(i,id);
        });
    }

    @Override
    public void addRolePermission(Set<Integer> ids, Integer id) {
        ids.forEach(i -> {
            systemMapper.addRolePermission(i,id);
        });
    }

    @Override
    public List<RolePermissionVO> getRolePermission(Integer i, Integer id) {
        return systemMapper.getRolePermission(i,id);
    }

    @Override
    public void changeRolePermissionStatus(Integer i, Integer id) {
        systemMapper.changeRolePermissionStatus(i,id);
    }

    @Override
    public void changePermissionMessage(Integer id,String msg,String url) {
        systemMapper.changePermissionMessage(id,msg,url);
    }
}
